//
//  CouponDatePickerView.m
//  SYPerfectDemo
//
//  Created by Lssyyy on 2019/8/8.
//  Copyright © 2019年 Lssyyy. All rights reserved.
//

#import "JMBCouponDatePickerView.h"
#import "UIButton+JMExtension.h"
#import "WSDatePickerView.h"
@interface JMBCouponDatePickerView()
@property (weak, nonatomic) IBOutlet UILabel *beginLabel;
@property (weak, nonatomic) IBOutlet UILabel *endLabel;
@property (weak, nonatomic) IBOutlet UIButton *beginButton;
@property (weak, nonatomic) IBOutlet UIButton *endButton;
@property (weak, nonatomic) IBOutlet UIView *pickViewBackView;
@property (nonatomic,strong) WSDatePickerView *datePicker;
@property (nonatomic,strong) NSDate *beginDate;
@property (nonatomic,strong) NSDate *endDate;
@property (assign,nonatomic) BOOL isEndTime;
@end
@implementation JMBCouponDatePickerView
- (void)awakeFromNib{
    [super awakeFromNib];
    _beginLabel.text = JMLocalStr(@"开始日期");
    [_beginButton setTitle:JMLocalStr(@"选择开始日期") forState:UIControlStateNormal];
    _pickViewBackView.backgroundColor = [UIColor orangeColor];
}


- (void)show{
    
    [[UIApplication sharedApplication].keyWindow addSubview:self];
    [self showDatePicker];
//    [UIView animateWithDuration:0.5 animations:^{
//
//    } completion:^(BOOL finished) {
//    }];
}

- (void)drawRect:(CGRect)rect{
    [super drawRect:rect];
    [self setFrame:CGRectMake(0, 0, UI_SCREEN_WIDTH, UI_SCREEN_HEIGHT)];
}

- (void)hide{
    [UIView animateWithDuration:0.5 animations:^{

    } completion:^(BOOL finished) {
        [self removeFromSuperview];
    }];
}

- (IBAction)cancelButtonAction:(UIButton *)sender {
    [_datePicker dismiss];
    [self hide];
}
- (IBAction)sureButtonAction:(UIButton *)sender {
    
    if ([_endDate isEarlierThanDate:_beginDate]) {
        NSLog(@"结束时间不能早于开始时间");
        return;
    }
    [_datePicker dismiss];
    [self hide];
}
- (IBAction)beginButtonAction:(UIButton *)sender {
    if (_isEndTime) {
        self.isEndTime = NO;
    }
    [self showDatePicker];
}
- (IBAction)endButtonAction:(UIButton *)sender {
    if (!_isEndTime) {
        self.isEndTime = YES;
    }
    [self showDatePicker];
}

- (void)showDatePicker{
   
    
    [_pickViewBackView addSubview:self.datePicker];
}

- (WSDatePickerView *)datePicker{
    if (!_datePicker) {
         WeakSelf
        _datePicker = [[WSDatePickerView alloc]initWithDateStyle:DateStyleShowYearMonthDay andFrame:_pickViewBackView.bounds CompleteBlock:^(NSDate *selectDate) {
            NSString *date = [selectDate stringWithFormat:@"yyyy-MM-dd"];
            StrongSelf
            if (self.isEndTime) {
                self.endDate = selectDate;
                [self.endButton setTitle:date forState:UIControlStateNormal];
            }else{
                self.beginDate = selectDate;
                [self.beginButton setTitle:date forState:UIControlStateNormal];
            }
            NSLog(@"选择的时间：%@",date);
        }];
        _datePicker.minLimitDate = [NSDate date];
    }
     if (self.isEndTime) {
         _datePicker.maxLimitDate = [NSDate date:@"2099-12-31" WithFormat:@"yyyy-MM-dd"];
         if (self.beginDate) {
             _datePicker.minLimitDate = self.beginDate;
         }else{
             _datePicker.minLimitDate = [NSDate date];
         }
     }
     else{
         _datePicker.minLimitDate = [NSDate date];
         if (self.endDate) {
             _datePicker.maxLimitDate = self.endDate;
         }else{
             _datePicker.maxLimitDate = [NSDate date:@"2099-12-31" WithFormat:@"yyyy-MM-dd"];
         }
     }
    return _datePicker;
}
@end
